//

#import "MB_AddViewController.h"
#import "MB_ChooseView.h"


@interface MB_AddViewController ()<UITextViewDelegate>
/** shuru*/
@property (nonatomic, strong) UITextView *mbtextField;
/** tishi*/
@property (nonatomic, strong) UILabel *plaLabel;
/** anniu*/
@property (nonatomic, strong) UIButton *sureButton;
/** */
@property (nonatomic, strong) MB_SqlView *sql;
/** tupian*/
@property (nonatomic, strong) UIImageView *imgsView;

@end

@implementation MB_AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navItem];
    // Do any additional setup after loading the view.
    _sql = [[MB_SqlView alloc]initWithData:@"FirstData" dataName:@"MoodData" success:^{} fail:^(NSError * _Nullable error) {}];
    
    [self.view addSubview:self.imgsView];
    [self.view addSubview:self.mbtextField];
    [self.mbtextField addSubview:self.plaLabel];
    if (self.mbtextField.text.length > 0) {
        self.plaLabel.text = @"";
    }
    [self.view addSubview:self.sureButton];
}
- (void)ActionbmBack {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)navItem {
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [button setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(ActionSetting) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = leftItem;
}

- (UITextView *)mbtextField {
    if (!_mbtextField) {
        _mbtextField = [[UITextView alloc]initWithFrame:CGRectMake(kRWidth(10), _height +kRHeight(20), kMainScreenWidth - kRWidth(20), kRHeight(300))];
        _mbtextField.textColor = RGB(145, 145, 145);
        _mbtextField.font = FontPFRegular(kRWidth(16));
        _mbtextField.delegate = self;
        _mbtextField.text = _model.content;
        //        _mbtextField.backgroundColor =
    }
    return _mbtextField;
}
- (UIImageView *)imgsView {
    if (!_imgsView) {
        _imgsView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight)];
        _imgsView.image = (UIImage*)_model.img;
    }
    return _imgsView;
}

- (UILabel *)plaLabel {
    if (!_plaLabel) {
        _plaLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 10, 300, 20)];
        _plaLabel.textColor = RGB(145, 145, 145);
        _plaLabel.font = FontPFRegular(kRWidth(16));
        _plaLabel.text = @"Input a content...";
    }
    return _plaLabel;
}
- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]initWithFrame:CGRectMake(kRWidth(50), CGRectGetMaxY(self.mbtextField.frame)+kRHeight(20), kMainScreenWidth - kRWidth(100), kRHeight(40))];
        [_sureButton setTitle:@"Save" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setBackgroundColor:RGB(255, 130 ,171)];
        _sureButton.layer.cornerRadius = 6;
        [_sureButton addTarget:self action:@selector(saveDatacreatt) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

-(void)textViewDidChange:(UITextView *)textView{
    //
    _plaLabel.text = @"";
    if (_mbtextField.text.length == 0) {
        _plaLabel.text = @"Input a content...";
    }
}

//
- (void)saveDatacreatt {
    if (_mbtextField.text.length <= 0) {
        return;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSString  *currentTimeString = [formatter stringFromDate:[NSDate date]];
    //
    NSMutableDictionary *parameter = [NSMutableDictionary new];
    [parameter setObject:currentTimeString forKey:@"date"];
    [parameter setObject:_mbtextField.text forKey:@"content"];
    if (self.imgsView.image != nil) {
        [parameter setObject:self.imgsView.image forKey:@"img"];
    }
    
    if (_model == nil) {
        [parameter setObject:[self randomnum] forKey:@"id"];
        
        [_sql mb_insertNewEntity:parameter success:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        } fail:^(NSError * _Nullable error) {}];
        
    }else{
        
        [_sql mb_updateEntity:parameter key:[NSString stringWithFormat:@"id=%@",_model.id] success:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        } fail:^(NSError * _Nullable error) {}];
        
    }
}
//
- (NSString*)randomnum {
    const int count = 6;
    NSString * source =@"125678934";
    NSMutableString *str=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[source length];
        NSString * s=[source substringWithRange:NSMakeRange(index, 1)];
        [str appendString:s];
    }
    NSLog(@"-%@",str);
    return str;
}
//MARK:设置
-(void)ActionSetting {
    MB_ChooseView *vc = [[MB_ChooseView alloc]init];
    [vc show];
    WeakObj(self);
    vc.sendImgBlock = ^(UIImage *img) {
        selfWeak.imgsView.image = img;
    };
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
